package com.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dao.IBisToolDao;
import com.model.Bis_tool;
import com.tx.Xpbistol.XpbistolI;
import com.tx.Xpbistol.XpbistolIA;
import com.tx.Xpbistol.XpbistolO;
import com.tx.Xpbistol.XpbistolOA;
import com.util.Util;

import static com.def.GenericDef.*;
@Repository
public class BisToolHibernateDao extends GenericHibernateDao<Bis_tool, String> 
	implements IBisToolDao{
	public XpbistolO queryByCondition(XpbistolI xpbistoolI){
		XpbistolO xpbisopeO = new XpbistolO();
		xpbisopeO.setTrx_id("XPBISTOL");
		xpbisopeO.setType_id("O");
		XpbistolIA iary = xpbistoolI.getIary().get(0);
		/***
		 * 支持 按ope_id,ope_ver 模糊查询
		 * ***/
		String tool_id  = iary.getTool_id();
		String hql ="FROM Bis_tool where 1=1 ";
		if(tool_id!=null){
			if(tool_id.indexOf('*')==-1){
				hql = hql + " and tool_id='" + tool_id + "'";
			}else{
				hql = hql + " and tool_id like '" + tool_id.replace('*', '%') + "'";
			}
		}
		if(iary.getTool_cate()!=null){
			hql = hql +" and tool_cate='" +iary.getTool_cate()+"'";
		}
//		if(ope_ver!=null){
//			if(ope_ver.indexOf('*')==-1){
//				hql = hql + " and ope_ver='" + ope_ver + "'";
//			}else{
//				hql = hql + " and ope_ver like '" + ope_ver.replace('*', '%') + "'";
//			}
//		}
		List<Bis_tool> bisToolList = this.find(hql);
//		if(bisToolList.size()==0){
//			xpbisopeO.setRtn_code(E_XPBISTOOL_NOT_FOUND_RECORDER +"");
//			xpbisopeO.setRtn_mesg("not found record ");
//			return xpbisopeO;
//			
//		}
		List<XpbistolOA> oaryList = new ArrayList<XpbistolOA>();
		for(Bis_tool bis_tool : bisToolList){
			XpbistolOA oary = new XpbistolOA();
			oary.setTool_id(bis_tool.getTool_id());
			oary.setRep_unit_fk(bis_tool.getRep_unit_fk());
			oary.setData_pat_fk(bis_tool.getData_pat_fk());
			oary.setMes_id_fk(bis_tool.getMes_id_fk());
			oary.setTool_dsc(bis_tool.getTool_dsc());
			oary.setUnit_typ(bis_tool.getUnit_typ());
			oary.setRoot_tool_id(bis_tool.getRoot_tool_id());
			oary.setTool_cate(bis_tool.getTool_cate());
			oary.setBay_id(bis_tool.getBay_id());
			oary.setFloor_code(bis_tool.getFloor_code());
			oary.setMax_sht_cnt(bis_tool.getMax_sht_cnt());
			oary.setMax_sht_time(bis_tool.getMax_sht_time());
			oary.setTool_trns_cate(bis_tool.getTool_trns_cate());
			oary.setTool_ip(bis_tool.getTool_ip());
			oary.setEvt_user(bis_tool.getEvt_user());
	        oaryList.add(oary);
		}
        xpbisopeO.setOary(oaryList);
        xpbisopeO.setTbl_cnt(oaryList.size());
        xpbisopeO.setRtn_code(RETURN_CODE_OK);
        xpbisopeO.setRtn_mesg("success");
		return xpbisopeO;
	}
	public XpbistolO addProc(XpbistolI xpbistoolI){
		XpbistolO xpbistolO = new XpbistolO();
		xpbistolO.setTrx_id("XPBISTOL");
		xpbistolO.setType_id("O");
		XpbistolIA iary = xpbistoolI.getIary().get(0);
		String tool_id = iary.getTool_id();
		Bis_tool queryBisOpe = getDataByKeyProc( tool_id );
		if(queryBisOpe!=null){
			xpbistolO.setRtn_code(E_XPBISTOOL_RECORDER_ALREADY_EXIST+"");
			xpbistolO.setRtn_code("Tool Already Exist");
			return xpbistolO;
		}
		try{
			Bis_tool bis_tool = new Bis_tool();
			bis_tool.setTool_id(tool_id);
			bis_tool.setRep_unit_fk(iary.getRep_unit_fk());//TODO:从哪获取
			bis_tool.setData_pat_fk(iary.getData_pat_fk());//TODO:从哪获取
			bis_tool.setMes_id_fk(iary.getMes_id_fk());//TODO:从哪获取
			bis_tool.setTool_dsc(iary.getTool_dsc());
			bis_tool.setUnit_typ(iary.getUnit_typ());
			bis_tool.setRoot_tool_id(iary.getRoot_tool_id());
			bis_tool.setTool_cate(iary.getTool_cate());
			bis_tool.setBay_id(iary.getBay_id());
			bis_tool.setFloor_code(iary.getFloor_code());
			bis_tool.setMax_sht_cnt(iary.getMax_sht_cnt());
			bis_tool.setMax_sht_time(iary.getMax_sht_time());
			bis_tool.setTool_ip(iary.getTool_ip());
			bis_tool.setTool_trns_cate(iary.getTool_trns_cate());
			bis_tool.setEvt_user(iary.getEvt_user());
			bis_tool.setEvt_timestamp(Util.getCurrentTimestamp());
			
	        this.save(bis_tool);
		}catch(Exception ex){
			logger.error(Util.stackTraceToString(ex));
		}
		
		xpbistolO.setRtn_code(RETURN_CODE_OK);
		xpbistolO.setRtn_mesg("success");
		return xpbistolO;
	}
	public XpbistolO updateProc(XpbistolI xpbistoolI){
		XpbistolO xpbistolO = new XpbistolO();
		xpbistolO.setTrx_id("XPBISTOL");
		xpbistolO.setType_id("O");
		XpbistolIA iary = xpbistoolI.getIary().get(0);
		String tool_id = iary.getTool_id();
		Bis_tool bis_tool = getDataByKeyProc( tool_id );
		if(bis_tool==null){
			xpbistolO.setRtn_code(E_XPBISOPE_NOT_FOUND_OPE+"");
			xpbistolO.setRtn_code("not found ope");
			return xpbistolO;
		}
		if(iary.getRep_unit_fk()!=null){
			bis_tool.setRep_unit_fk(iary.getRep_unit_fk());
		}
	    if(iary.getData_pat_fk()!=null){
	    	bis_tool.setData_pat_fk( iary.getData_pat_fk());
	    }
		if(iary.getMes_id_fk()!=null){
			bis_tool.setMes_id_fk(iary.getMes_id_fk());
		}
		if(iary.getTool_dsc()!=null){
			bis_tool.setTool_dsc(iary.getTool_dsc());
		}
		if(iary.getUnit_typ()!=null){
			bis_tool.setUnit_typ(iary.getUnit_typ());
		}
        if(iary.getRoot_tool_id()!=null){
        	bis_tool.setRoot_tool_id(iary.getRoot_tool_id());
        }
        if(iary.getTool_cate()!=null){
        	bis_tool.setTool_cate(iary.getTool_cate());
        }
        if(iary.getBay_id()!=null){
        	bis_tool.setBay_id(iary.getBay_id());
        }
        if(iary.getFloor_code()!=null){
        	bis_tool.setFloor_code(iary.getFloor_code());
        }
        if(iary.getMax_sht_cnt()!=null){
        	bis_tool.setMax_sht_cnt(iary.getMax_sht_cnt());
        }
        if(iary.getMax_sht_time()!=null){
        	bis_tool.setMax_sht_time(iary.getMax_sht_time());
        }
        if(iary.getTool_trns_cate()!=null){
        	bis_tool.setTool_trns_cate(iary.getTool_trns_cate());
        }
        if(iary.getTool_ip()!=null){
        	bis_tool.setTool_ip(iary.getTool_ip());
        }
        if(iary.getEvt_user()!=null){
        	bis_tool.setEvt_user(iary.getEvt_user());
        }
        bis_tool.setEvt_timestamp(Util.getCurrentTimestamp());
        this.update(bis_tool);
        xpbistolO.setRtn_code(RETURN_CODE_OK);
        xpbistolO.setRtn_mesg("success");
		return xpbistolO;
		
	}
	public XpbistolO deleteProc(XpbistolI xpbistoolI){
		XpbistolO xpbistolO = new XpbistolO();
		xpbistolO.setTrx_id("XPBISTOL");
		xpbistolO.setType_id("O");
		XpbistolIA iary = xpbistoolI.getIary().get(0);
		String tool_id = iary.getTool_id();
		Bis_tool bis_tool = getDataByKeyProc(tool_id);
		if(bis_tool==null){
			xpbistolO.setRtn_code(E_XPBISTOOL_NOT_FOUND_RECORDER+"");
			xpbistolO.setRtn_code("not found tool_id["+tool_id+"]");
			return xpbistolO;
		}
		this.delete(bis_tool);
		xpbistolO.setRtn_code(RETURN_CODE_OK);
		xpbistolO.setRtn_mesg("success");
		return xpbistolO;
	}
	public Bis_tool getDataByKeyProc(String tool_id){
		Bis_tool bis_tool = this.get(tool_id);
		return bis_tool;
	}
	public XpbistolO listAllFnc(XpbistolI xpbistoolI){
		XpbistolO xpbistolO = new XpbistolO();
		xpbistolO.setTrx_id("XPBISTOL");
		xpbistolO.setType_id("O");
		List<XpbistolOA> oaryList = new ArrayList<XpbistolOA>();
		String hql = "FROM Bis_tool where 1=1";
		List<Bis_tool> bisToolList = this.find(hql);
		for(Bis_tool bis_tool:bisToolList){
			XpbistolOA oary = new XpbistolOA();
			oary.setTool_id(bis_tool.getTool_id());
			oary.setTool_dsc(bis_tool.getTool_dsc());
			oary.setBay_id(bis_tool.getBay_id());
			oaryList.add(oary);
		}
		xpbistolO.setOary(oaryList);
		xpbistolO.setTbl_cnt(oaryList.size());
		xpbistolO.setRtn_code(RETURN_CODE_OK);
		xpbistolO.setRtn_mesg("success");
		return xpbistolO;
	}

}
